package com.kyzh.core.adapters

import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.atools.util.setHeight
import com.gushenge.atools.util.setWidth
import com.gushenge.core.beans.Game
import com.kyzh.core.R
import com.kyzh.core.databinding.FragSortRightItemJingxuanBinding
import com.kyzh.core.extension.isNotNullOrEmpty
import com.kyzh.core.utils.SampleCoverVideo
import com.kyzh.core.utils.setVisibility
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView
import org.jetbrains.anko.dip
import org.jetbrains.anko.textColor

class SortChosenAdapter1() :
    BaseQuickAdapter<Game, BaseDataBindingHolder<FragSortRightItemJingxuanBinding>>(R.layout.frag_sort_right_item_jingxuan),
    LoadMoreModule {
    override fun convert(
        holder: BaseDataBindingHolder<FragSortRightItemJingxuanBinding>,
        item: Game
    ) {
        holder.dataBinding?.data = item
        holder.apply {
            setText(R.id.tv_tip, "  ${item.point}分   ${item.fenlei}   ${item.size}")
            val bq = getView<LinearLayout>(R.id.bq)
            holder.dataBinding?.apply {
                tvTip.text = "  ${item.point}分   ${item.fenlei}   ${item.size}"

                if (item.zhekou.isNullOrEmpty()) {
                    btDiscount.setVisibility(false)
                } else {
                    if (!item.zhekou.contains("折")) {
                        btDiscount.text = item.zhekou + "折"
                    } else {
                        btDiscount.text = item.zhekou
                    }
                    btDiscount.setVisibility(true)
                    if (item.zhekou.contains("10")) {
                        btDiscount.setVisibility(false)
                    }
                }


                if (!item.biaoqian.isNullOrEmpty()) {
                    bq.removeAllViews()
                    var i = 0
                    item.biaoqian.forEach { text ->
                        val textView = TextView(context)
                        textView.setPadding(
                            context.dip(4),
                            context.dip(2),
                            context.dip(4),
                            context.dip(2)
                        )
                        textView.gravity = Gravity.CENTER
                        textView.textSize = 10f
                        textView.text = text
                        val lp = LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT
                        )
                        lp.setMargins(0, 0, 10, 0)
                        textView.layoutParams = lp
                        when (i) {
                            0 -> {
                                textView.background = context.resources.getDrawable(R.drawable.bq1)
                                textView.textColor = context.resources.getColor(R.color.bq1)
                            }

                            1 -> {
                                textView.background = context.resources.getDrawable(R.drawable.bq2)
                                textView.textColor = context.resources.getColor(R.color.bq2)
                            }

                            2 -> {
                                textView.background = context.resources.getDrawable(R.drawable.bq3)
                                textView.textColor = context.resources.getColor(R.color.bq3)
                            }

                            else -> {
                                textView.background = context.resources.getDrawable(R.drawable.bq1)
                                textView.textColor = context.resources.getColor(R.color.bq1)
                            }
                        }
                        bq.addView(textView)
                        i++
                    }
                } else {
                    bq.setVisibility(false)
                }
                if (item.video_url.isNotNullOrEmpty()) {
                    setVisible(R.id.bigImg, false)
                    setVisible(R.id.videoPlayer, true)
                    val videoPlayer = getView<SampleCoverVideo>(R.id.videoPlayer)
                    videoPlayer.apply {
                        if (item.video_img.isNotNullOrEmpty()) {
                            val imageView = ImageView(context)
                            Glide.with(context).load(item.video_img).apply(
                                RequestOptions()
                                    .transforms(
                                        CenterCrop(), RoundedCorners(20)
                                    )
                            ).into(imageView)
                            thumbImageView = imageView

                        }
                        playPosition = holder.layoutPosition
                        playTag = "SortChosenAdapter"
                        isNeedShowWifiTip = false
                        isLooping = true
                        dismissControlTime = 2000
                        setIsTouchWiget(true)
                        setThumbPlay(true)
                        backButton.visibility = View.GONE
                        backButton.setOnClickListener {
                            GSYVideoManager.backFromWindowFull(context)
                        }
                        fullscreenButton.setOnClickListener {
                            backButton.visibility = View.VISIBLE
                            startWindowFullscreen(context, false, false)
                        }
                        startButton.setOnClickListener {
                            when (currentState) {
                                GSYVideoView.CURRENT_STATE_PAUSE -> {
                                    onVideoResume(false)
                                }

                                GSYVideoView.CURRENT_STATE_PLAYING -> {
                                    onVideoPause()
                                }

                                else -> {
                                    startPlayLogic()
                                }
                            }
                        }
                        isLockLand = true;
                        isAutoFullWithSize = false
                        isShowFullAnimation = true;
                        backButton.visibility = View.GONE;
                        fullscreenButton.setOnClickListener {
                            //第一个true是否需要隐藏actionbar，第二个true是否需要隐藏statusbar
                            videoPlayer.startWindowFullscreen(context, true, true);
                        }
                        setVideoAllCallBack(object : GSYSampleCallBack() {
                            override fun onPrepared(url: String?, vararg objects: Any?) {
                                super.onPrepared(url, *objects)
                                val full: Boolean =
                                    videoPlayer.isIfCurrentIsFullscreen()
                                if (!videoPlayer
                                        .isIfCurrentIsFullscreen()
                                ) {
    //                                GSYVideoManager.instance().isNeedMute = true
                                }
                                if (videoPlayer.isIfCurrentIsFullscreen()) {
                                    GSYVideoManager.instance().setLastListener(videoPlayer)
                                }
                            }

                            override fun onEnterFullscreen(url: String?, vararg objects: Any?) {
                                super.onEnterFullscreen(url, *objects)
    //                            GSYVideoManager.instance().isNeedMute = false
                            }

                            override fun onQuitFullscreen(url: String?, vararg objects: Any?) {
                                super.onQuitFullscreen(url, *objects)
    //                            GSYVideoManager.instance().isNeedMute = true
                            }
                        })
                    }
                    videoPlayer.setUp(item.video_url, true, "")
                } else if (!item.video_img.isNullOrEmpty()) {
                    setVisible(R.id.bigImg, true)
                    setVisible(R.id.videoPlayer, false)
                } else {
                    setVisible(R.id.bigImg, false)
                    setVisible(R.id.videoPlayer, false)
                }
            }


        }
    }
}
